//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CraigSneydScheme.h"
using namespace Cephei::QL::Methods::Finitedifferences::Schemes;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpComposite.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (Double theta, Double mu, Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpComposite^ map) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmLinearOpComposite^ _Cmap;
    try
    {
#ifdef HANDLE
        _phCraigSneydScheme = NULL;
#endif
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta); //d
        QuantLib::Real _mu = (QuantLib::Real)ValueHelper::Convert (mu); //d
        _Cmap = safe_cast<CFdmLinearOpComposite^> (map);
        _Cmap->Lock();
        boost::shared_ptr<QuantLib::FdmLinearOpComposite>& _map = static_cast<boost::shared_ptr<QuantLib::FdmLinearOpComposite>&> (_Cmap->GetShared ()); 
        _ppCraigSneydScheme = new boost::shared_ptr<QuantLib::CraigSneydScheme> (new QuantLib::CraigSneydScheme ( _theta,  _mu,  _map ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmap != nullptr) _Cmap->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (boost::shared_ptr<QuantLib::CraigSneydScheme>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCraigSneydScheme = NULL;
#endif
	_ppCraigSneydScheme = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (QuantLib::CraigSneydScheme& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCraigSneydScheme = NULL;
#endif
	_ppCraigSneydScheme = new boost::shared_ptr<QuantLib::CraigSneydScheme> (&childNative);
    
    _CraigSneydSchemeOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (CCraigSneydScheme^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCraigSneydScheme = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCraigSneydScheme = new boost::shared_ptr<QuantLib::CraigSneydScheme> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCraigSneydScheme = NULL;
#endif
	if (!t->IsSubclassOf(CCraigSneydScheme::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (QuantLib::Handle<QuantLib::CraigSneydScheme>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCraigSneydScheme = &childNative;
	_ppCraigSneydScheme = &static_cast<boost::shared_ptr<QuantLib::CraigSneydScheme>>(childNative.currentLink());
    
    _CraigSneydSchemeOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (QuantLib::Handle<QuantLib::CraigSneydScheme> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCraigSneydScheme = &childNative;
	_ppCraigSneydScheme = &static_cast<boost::shared_ptr<QuantLib::CraigSneydScheme>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::CCraigSneydScheme (QuantLib::CraigSneydScheme childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCraigSneydScheme = NULL;
#endif
	_ppCraigSneydScheme = new boost::shared_ptr<QuantLib::CraigSneydScheme> (new QuantLib::CraigSneydScheme (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::~CCraigSneydScheme ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCraigSneydScheme != NULL)
    {
	    delete _ppCraigSneydScheme;
        _ppCraigSneydScheme = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::!CCraigSneydScheme ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCraigSneydScheme != NULL)
    {
	    delete _ppCraigSneydScheme;
    }
}
QuantLib::CraigSneydScheme& Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::GetReference ()
{
    if (_ppCraigSneydScheme == NULL) throw REFNEW NativeNullException ();
	return **_ppCraigSneydScheme;
}
boost::shared_ptr<QuantLib::CraigSneydScheme>& Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::GetShared ()
{
    if (_ppCraigSneydScheme == NULL) throw REFNEW NativeNullException ();
	return *_ppCraigSneydScheme;
}
QuantLib::CraigSneydScheme* Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::GetPointer ()
{
    if (_ppCraigSneydScheme == NULL) throw REFNEW NativeNullException ();
	return &**_ppCraigSneydScheme;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CraigSneydScheme>& Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::GetHandle ()
{
	if (_phCraigSneydScheme == NULL)
	{
		_phCraigSneydScheme = new Handle<QuantLib::CraigSneydScheme> (*_ppCraigSneydScheme);
	}
	return *_phCraigSneydScheme;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::HasNative () 
{
	return (_ppCraigSneydScheme != NULL);
}

Cephei::QL::Methods::Finitedifferences::Schemes::ICraigSneydScheme^ Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme::SetStep (Double dt)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
    	(*_ppCraigSneydScheme)->setStep ( _dt );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Schemes::ICraigSneydScheme^ Cephei::QL::Methods::Finitedifferences::Schemes::CCraigSneydScheme_Factory::Create (Double theta, Double mu, Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpComposite^ map)
{
    return REFNEW CCraigSneydScheme ( theta,  mu,  map);
}
